var express = require('express');
var router = express.Router();
var session = require('express-session');
var moment = require('moment');
var async = require('async');
// var fs = require('fs');
// var    path = require('path');

var db = require('./../../utils/db.js');

//进入文档列表页面

router.get('/list', function (req, res, next) {
    res.render('daily/word/index');
});


//获取文章数据
router.get('/datagrid', function (req, res, next) {
    var typeCode = req.query.typeCode ? req.query.typeCode.trim() : '';

    var sql = "select *, (select person_name from base_user u where u.id=w.word_author) as word_author_name from daily_word w where 1=1";
    if (typeCode) {
        sql = "select *, (select person_name from base_user u where u.id=w.word_author) as word_author_name from daily_word w left join daily_word_type as dwt on dwt.id = w.type_id where dwt.code like '" + typeCode +"%'";
    }
    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            var wordList = [];
            for (var i = 0; i < rows.length; i++) {
                wordList.push({
                    id: rows[i].id,
                    word_title: rows[i].word_title,
                    word_time: moment(rows[i].word_time).format('YYYY-MM-DD HH:mm:ss'),
                    word_author: rows[i].word_author_name
                })
            }
            res.json({
                errcode: 0,
                massage: "获取成功",
                data: wordList
            });
        } else {
            res.json({
                errcode: 1,
                massage: "获取失败"
            });
        }
    });

});

// 新增文档
router.get("/add", function (req, res, next) {
    var typeId = req.query.typeId ? req.query.typeId.trim() : '';
    res.render('daily/word/edit', {
        id: '',
        wordTitle: '',
        wordTime: '',
        wordAuthor: '',
        wordContent: '',
        typeId: typeId
    });
});
//查看详情

router.get("/detail/:id", function (req, res, next) {
    var id = req.params.id;
    var sql = "select *, (select person_name from base_user u where u.id=w.word_author) as word_author_name from daily_word w where id =" + id;
    db.query(sql, function (err, rows) {
        if (rows && rows.length > 0) {
            res.render('daily/word/detail', {
                id: rows[0].id,
                wordTitle: rows[0].word_title,
                wordTime: moment(rows[0].word_time).format('YYYY-MM-DD HH:mm:ss'),
                wordAuthor: rows[0].word_author_name,
                wordContent: rows[0].word_content,
                wordHtml: rows[0].word_html,
                typeId: rows[0].type_id
            });
        }
    });

});
//修改文章
router.get("/edit/:id", function (req, res, next) {
    var id = req.params.id;
    var sql = "select * from daily_word where id=" + id;
    db.query(sql, function (err, rows) {
        if (rows && rows.length > 0) {
            res.render('daily/word/edit', {
                id: rows[0].id,
                wordTitle: rows[0].word_title,
                wordTime: rows[0].word_time,
                wordAuthor: rows[0].word_author,
                wordContent: rows[0].word_content,
                wordHtml: rows[0].word_html,
                typeId: rows[0].type_id
            });
        }
    });
});

//删除
router.get("/delete/:id", function (req, res) {
    var id = req.params.id;
    var sql = "delete from daily_word where id =" + id;
    db.query(sql, function (err, rows) {
        if (err) {
            res.send("删除失败" + err);
        }
        else {
            res.redirect("/daily/word/list");
        }
    });
});
//保存
router.post('/save', function (req, res, next) {
    var id = req.body.id ? req.body.id.trim() : '';
    var typeId = req.body.typeId ? req.body.typeId.trim() : '';
    var wordTitle = req.body.wordTitle ? req.body.wordTitle.trim() : '';
    var wordContent = req.body.wordContent ? req.body.wordContent.trim() : '';
    var wordHtml = req.body.wordHtml ? req.body.wordHtml.trim() : '';

    var loginedUser = req.session.loginedUser;
    var wordAuthorId = loginedUser.id;
    var wordTime = moment().format('YYYY-MM-DD HH:mm:ss');

    var sql = '';
    if (id) {
        sql = "update daily_word set word_title = '" + wordTitle + "',word_content = '" + wordContent + "',word_html = '" + wordHtml + "',type_id ='" + typeId + "' where id =" + id;
    } else {
        sql = "insert into daily_word(word_title,word_time,word_author,word_content,word_html,type_id)" + "values('" + wordTitle + "','" + wordTime + "'," + wordAuthorId + ",'" + wordContent + "','" + wordHtml + "','" + typeId + "')";
    }
    db.query(sql, function (err, rows) {
        if (rows) {
            res.json({
                errcode: 0,
                massage: "保存成功"
            });
        } else {
            res.json({
                errcode: 1,
                massage: "保存失败"
            });
        }
    });

});


module.exports = router;